﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Zatify.WebUI.Infrastructure;
using Zatify.Domain.Abstract;
using Zatify.WebUI.Models;

namespace Zatify.WebUI.Controllers
{
    [Authorize]
    public class BillingController : BaseController
    {
        private IZatifyRepository repository;
        public BillingController(IZatifyRepository repository)
        {
            this.repository = repository;
        }

        private int pageSize = 10;

        public ActionResult TransactionHistory(int? promotionId, DateTime? dateOperation, int page = 1)
        {
            var result = this.repository.BillingTransactionHistoryGet(User.SecurityToken.Value, promotionId, dateOperation);

            return View(new BillingTransactionHistoryViewModel()
            {
                PromotionId = promotionId,
                BillingTransactionHistoryList = result.Skip((page - 1) * pageSize).Take(pageSize).ToList(),
                PagingInfo = new PagingInfo()
                {
                    CurrentPage = page,
                    ItemsPerPage = pageSize,
                    TotalItems = result.Count
                }
            });
        }

        public ActionResult PromotionTransactionHistory(int promotionId, int page = 1)
        {
            var result = this.repository.BillingTransactionHistoryForPromotionGet(this.User.SecurityToken.Value, promotionId, null);

            var promoName = this.repository.PromotionGetById(promotionId).Name;

            return View(new BillingTransactionHistoryForPromotionViewModel()
            {
                PromotionName = promoName,
                BillingTransactionHistoryForPromotionList = result.ToList()
            });
        }

        //public ActionResult TransactionDetails(DateTime? date, int? promotionId, int? transactionId)
        //{
        //    if (date.HasValue && promotionId.HasValue)
        //    {
        //        var result = this.repository.BillingTransactionDetails_DeditCard(User.SecurityToken.Value, promotionId.Value, date.Value);

        //        return View(new BillingTransactionDetailsViewModel()
        //        {
        //            IsCredit = false,
        //            TransactionId = result.TransactionId, 
        //            Date = result.Date.Value,
        //            FlatFee = result.Description,
        //            //Type = result.Type,
        //            Amount = result.Amount,
        //            Balance = result.Balance
        //            //Pushes = result.Pushes.Value,
        //            //RatePerPush = result.RatePerPush.Value,
        //            //PromotionName = result.PromotionName
        //        });
        //    }
        //    else if (transactionId.HasValue)
        //    {
        //        var result = this.repository.BillingTransactionDetails_CreditCard(User.SecurityToken.Value, transactionId.Value);

        //        if (result != null)
        //        {
        //            return View(new BillingTransactionDetailsViewModel()
        //            {
        //                IsCredit = true,
        //                Date = result.Date,
        //                //Type = result.Type,
        //                Amount = result.Amount,
        //                ConfirmationNo = result.ConfirmationNo,
        //                FlatFee = result.Description,
        //                PromotionName = result.PromotionName
        //            });
        //        }

        //        return RedirectToAction("TransactionHistory", new { promotionId = promotionId, page = 1 });
        //    }

        //    return View();
        //}
    }
}
